#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#define FIFO_FILE   "/tmp/nsgbot_udpQ"
#define FIFO_DNS    "/tmp/nsgbot_dnsQ"

#include "../nsgbot_capture/pseudo_pkt.h"

#define TAG_DNS_Response 2000001
#define TAG_DNS_Query 2000002

int main( void)
{
  int   fd;
  int dns_fd;
  pseudo_pkt *ppkt;
  
  if ( -1 == ( fd = open( FIFO_FILE, O_RDWR))) 
	{
	  perror( "open() name pipe");
	  exit(1);
	}

  if ( -1 == ( dns_fd = open( FIFO_DNS, O_RDWR)))
	{
	  perror( "open() named pipe : nsgbot_dnsQ");
	  exit(1);
	}

  // temp memory
  if ( (ppkt = (pseudo_pkt*)calloc(1, sizeof(pseudo_pkt))) == NULL) {
	fprintf(stderr, "Calloc error in nsgbot_tcp\n");
	exit;
  }

  while( 1 )
	{
	  read( fd, ppkt, sizeof(pseudo_pkt));
	  //printf( "%d\n", counter++);
	  if (ppkt->dport == 53) {
		ppkt->tag = TAG_DNS_Query;
		write(dns_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  else if (ppkt->sport == 53) {
		ppkt->tag = TAG_DNS_Response;
		write(dns_fd, (void*)ppkt, sizeof(pseudo_pkt));
		continue;
	  }
	  //print_pkt(&temp, 0);
	}
  close( fd);
}
